package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.service.AddressService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Reference
    private AddressService addressService;

   //查询全部
    @GetMapping("/findAll")
    protected Result selectAll(){
        try {
            List<Address> list = addressService.findAll();
            return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }

    //单个查询
    @GetMapping("{id}")
    protected Result findById(@PathVariable Integer id) {
        try {
            Address byId = addressService.findById(id);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS,byId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }


   //添加
    @PostMapping("/submit")
    protected Result addAddress(@RequestBody Address address) {
        try {
        if (address.getId()==null){
            addressService.add(address);
        }else{
            addressService.update(address);
        }
            return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);
        }
    }

   //修改
    @PutMapping("/update/{checkitemIds}")
    protected Result updateAddress(@RequestBody Address address) {
        try {
            addressService.update(address);

            return new Result(true, MessageConstant.EDIT_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ADDRESS_FAIL);
        }
    }

    //删除
    @DeleteMapping("/{id}")
    protected Result deleteAddress(@PathVariable(value = "id") Integer id) {
        try {
            addressService.delete(id);
            return new Result(true, MessageConstant.DELETE_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ADDRESS_FAIL);
        }
    }



    //修改启用状态
    @PutMapping("/{id}/{enable}")
    protected Result change(@PathVariable Integer id,@PathVariable Integer enable) {

        System.out.println(id);
        try {
           Address address=new Address();
           address.setId(id);
           address.setEnable(enable);

           addressService.change(address);
            return new Result(true, MessageConstant.EDIT_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ADDRESS_FAIL);
        }
    }
}



